# Maintainer: Timo Teräs <timo.teras@iki.fi>
pkgname=raspberrypi-userland
pkgver=0.20231020
pkgrel=2
_commit="96a7334ae9d5fc9db7ac92e59852377df63f1848"
pkgdesc="Raspberry Pi tools for interfacing to GPU (deprecated)'"
url="https://github.com/raspberrypi/userland"
arch="armhf armv7 aarch64"
license="BSD-3-Clause"
provides="raspberrypi=$pkgver-r$pkgrel"  # for backward compatibility
replaces="raspberrypi"  # for backward compatibility
depends_dev="linux-headers $pkgname-static $pkgname-libs"
makedepends="cmake samurai $depends_dev"
options="!fhs !check"
subpackages="$pkgname-dev $pkgname-doc $pkgname-static $pkgname-libs $pkgname-udev:udev"
source="$pkgname-$_commit.tar.gz::https://github.com/raspberrypi/userland/archive/$_commit.tar.gz
	dont-build-moved-utils.patch
	basename.patch
	10-local-rpi.rules
	"
builddir="$srcdir/userland-$_commit"

prepare() {
	default_prepare
	sed -i 's/ bash / sh /g' host_applications/linux/apps/raspicam/CMakeLists.txt
}

build() {
	case "$CARCH" in
		arm*) local _arm64=off;;
		aarch64) local _arm64=on;;
	esac
	cmake -B build -G Ninja \
		-DCMAKE_C_FLAGS="$CFLAGS -D_GNU_SOURCE" \
		-DARM64=$_arm64 \
		-DCMAKE_BUILD_TYPE=MinSizeRel \
		-DCMAKE_INSTALL_RPATH=/opt/vc/lib \
		-DCMAKE_SHARED_LINKER_FLAGS="-Wl,--no-as-needed"
	cmake --build build
}

package() {
	DESTDIR="$pkgdir" cmake --build build --target install

	# nuke the unwanted stuff
	rm -rf "$pkgdir"/opt/vc/src

	# move main binaries to /usr/bin (purposely omit those moved into utils package)
	mkdir -p "$pkgdir"/usr/bin
	for _app in mmal_vc_diag raspistill raspivid raspividyuv raspiyuv vcsmem \
		tvservice vchiq_test; do
			[ -f "$pkgdir"/opt/vc/bin/"$_app" ] && mv "$pkgdir"/opt/vc/bin/"$_app" "$pkgdir"/usr/bin/"$_app"
	done

	install -Dm644 -t "$pkgdir"/usr/lib/udev/rules.d/ "$srcdir"/10-local-rpi.rules

	mkdir -p "$pkgdir"/usr/share
	mv "$pkgdir"/opt/vc/man "$pkgdir"/usr/share
}

static() {
	pkgdesc="Static files for $pkgname (deprecated)"
	provides="raspberrypi-static=$pkgver-r$pkgrel"  # for backward compatibility
	replaces="raspberrypi-static"  # for backward compatibility

	amove opt/vc/lib/*.a
}

libs() {
	pkgdesc="Raspberry Pi libraries for interfacing to GPU (deprecated)"
	provides="raspberrypi-libs=$pkgver-r$pkgrel"  # for backward compatibility
	replaces="raspberrypi-libs"  # for backward compatibility

	amove opt/vc/lib
}

dev() {
	pkgdesc="Raspberry Pi tools for interfacing to GPU (deprecated development files)"
	provides="raspberrypi-dev=$pkgver-r$pkgrel"  # for backward compatibility
	replaces="raspberrypi-dev"  # for backward compatibility
	default_dev
	amove opt/vc/lib/pkgconfig
}

udev() {
	pkgdesc="VC4 GPU vchiq udev rules"
	install="$subpkgname.post-install"
	install_if="$pkgname-libs=$pkgver-r$pkgrel eudev"
	depends=""

	amove usr/lib/udev/rules.d
}

doc() {
	provides="raspberrypi-doc=$pkgver-r$pkgrel"  # for backward compatibility
	replaces="raspberrypi-doc"  # for backward compatibility

	default_doc
}

sha512sums="
246a547cd411f893fe903b92a362b27467e2a3d7f3fd8bab6d9dc6f826d09781a341482826926bd3e94b7609c6a1a8a05726548e6e577f5b64d2130cc3e0fb71  raspberrypi-userland-96a7334ae9d5fc9db7ac92e59852377df63f1848.tar.gz
b9bc1dd0c596b81eeab90238685b7bc6b6a7d8606467d7444892dd4b2f0f53c43964087f3a2f5c8072fcc90549476453331542f9eff44c883e0c6fc2be8d7ad5  dont-build-moved-utils.patch
c21a53d8366b75ea2091ec4a70d0df0bde02483964121cab235a703d44e4a3bca13af79cc14b4b0d28a74bad460d2281c26f2b9b6dbe3b4ff1e3e107acce2dca  basename.patch
256d1faa31ff43fbbc0fbbf814cb92aaa66f3fc1630e2260fefa6d206baa3bf2c98fc44aefb70a34ea0a092bf208b151a9c0ed1dc0f140fcc59d08ca1a629d6a  10-local-rpi.rules
"
